﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using GestionPedago.EntityContext;
using GestionPedago.EntityContext.EntityOPTools;

namespace Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de l'Entity Stagiaire
    /// </summary>
    public abstract class StagiaireOPManager
    {

        /// <summary>
        /// Méthode qui récupère un stagiaire par son code
        /// </summary>
        /// <param name="code">Le code du stagiaire</param>
        /// <returns>Le stagiaire trouvé</returns>
         public static Stagiaire GetStagiaireByCode(int code)
         {

             try
             {
                 var query = from it in EntityConnection.GetContext().Stagiaire
                             where it.CodeStagiaire == code
                             select it;

                 Stagiaire stagiaire = null;

                 stagiaire = query.First();

                 return stagiaire;

             }
             catch (Exception ex)
             {
                 throw ex;
             }

         }

        /// <summary>
        /// Méthode qui retourne une liste de stagiaires par critère
        /// </summary>
        /// <param name="nom">Le nom</param>
        /// <param name="prenom">Le prénom</param>
        /// <param name="ville">La ville</param>
        /// <param name="codeRegion">La région</param>
        /// <param name="codePostal">Le code postal</param>
        /// <param name="dateCreation">La date de création</param>
        /// <param name="p">La promotion</param>
        /// <param name="e">L'entreprise</param>
        /// <param name="orderByNomPrenomAsc">Le sens du tri</param>
        /// <returns></returns>
         public static List<Stagiaire> GetListeStagiaire(string nom = null, string prenom = null, string ville = null, string codeRegion = null,string codePostal = null, DateTime? dateCreation = null,Promotion p = null,Entreprise e = null, bool orderByNomPrenomAsc = true)
         {

             try
             {
                 StagiaireParEntreprise spe = new StagiaireParEntreprise();
                 spe.Entreprise = e;
                 PlanningIndividuelFormation pif = new PlanningIndividuelFormation();
                 pif.Promotion = p;
                 IQueryable<Stagiaire> query = EntityConnection.GetContext().Stagiaire;
                 query = query.Where(it => (it.Nom.ToUpper().Contains(nom.ToUpper()) || (nom == null || nom.Trim() == string.Empty))
                             && (it.Prenom.ToUpper().Contains(prenom.ToUpper()) || (prenom == null || prenom.Trim() == string.Empty))
                             && (it.Ville.ToUpper().Contains(ville.ToUpper()) || (ville == null || ville.Trim() == string.Empty))
                             && (it.CodeRegion == codeRegion || codeRegion == "__")
                             && (it.Codepostal.ToUpper().Contains(codePostal.ToUpper()) || (codePostal == null || codePostal.Trim() == string.Empty))
                             && (dateCreation == null || it.DateCreation >= dateCreation)
                            
                 );
                 

                 if (orderByNomPrenomAsc)
                 {
                     query = query.OrderBy(it => it.Nom).ThenBy(it => it.Prenom);
                 }
                 else
                 {
                     query = query.OrderByDescending(it => it.Nom).ThenByDescending(it => it.Prenom);
                 }
                /* query.Select(it => it);*/
     
                 List<Stagiaire> lst = new List<Stagiaire>();

                 List<Stagiaire> listeStagiaires = query.ToList();
                 foreach(Stagiaire s in listeStagiaires)
                 {
                     if ((p == null || s.PlanningIndividuelFormation.Contains(pif, new PlanningIndividuelFormationComparerOnPromotion())||(p!=null && IsStagiaireInscritCoursPromotion(s.CodeStagiaire,p.CodePromotion))) 
                            && (e == null || s.StagiaireParEntreprise.Contains(spe, new StagiaireParEntrepriseComparerOnEntreprise())))
                        lst.Add(s);
                 }

                 return lst;

             }
             catch (Exception ex)
             {
              //   Console.WriteLine(ex.Message);
                 throw ex;
             }
             
         }


         public static bool IsStagiaireInscritCoursPromotion(int codeStagiaire, string codePromotion){
             bool res = false;
             IQueryable<PlanningIndividuelDetail> query;
             query = EntityConnection.GetContext().PlanningIndividuelDetail;
             query = query.Where(it => it.PlanningIndividuelFormation.CodeStagiaire == codeStagiaire && it.Cours.CodePromotion == codePromotion);
             if(query.Count() > 0) {
                 res = true;
             } else {
                 res = false;
             }
             return res;
         }
    }
}
